<html>
<head>
	<meta charset="UTF-8">
	<meta content="IE=edge" http-equiv="X-UA-Compatible">
	<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width" name="viewport">
	<title>1484：[HNOI2009]通往城堡之路</title>
	<!-- css -->
	<link href="../css/base.min.css" rel="stylesheet">
	<link href="../css/project.min.css" rel="stylesheet">
	
	<!-- favicon -->
	<!-- ... -->
</head>
<body class="page-brand">
	<header class="header header-transparent header-waterfall ui-header">
		<ul class="nav nav-list pull-left">
			<li>
				<a data-toggle="menu" href="#menu">
					<span class="icon icon-lg">menu</span>
				</a>
			</li>
		</ul>
		<a class="header-logo header-affix-hide margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">[HNOI2009]通往城堡之路</a>
		<span class="header-logo header-affix margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">[HNOI2009]通往城堡之路</span>
	</header>
	<nav aria-hidden="true" class="menu" id="menu" tabindex="-1">
		<div class="menu-scroll">
			<div class="menu-content">
				<a class="menu-logo" href="../index.html">BZOJ离线题库</a>
				<ul class="nav">
					<li>
						<a class="waves-attach" data-toggle="collapse" href="#problems">题目</a>
						<ul class="menu-collapse collapse in" id="problems">
							<li>
								<a class="waves-attach" href="../index.html">主页</a>
							</li>
							<li>
								<a class="waves-attach" href="../list.html">题目列表</a>
							</li>
						</ul>
					</li>
					<li>
						<a class="collapsed waves-attach" data-toggle="collapse" href="#about">关于</a>
						<ul class="menu-collapse collapse" id="about">
							<li>
								<a class="waves-attach" href="../about.html">关于此项目</a>
							</li>
						</ul>
					</li>
					
				</ul>
			</div>
		</div>
	</nav>
	<main class="content">
		<div class="content-header ui-content-header">
			<div class="container">
				<h1 class="content-heading">
                [HNOI2009]通往城堡之路                </h1>
                <p>时间限制：10s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  空间限制：64MB</p>			</div>
		</div>
		<div class="container">
			<section class="content-inner margin-top-no">
				<div class="row">
					<div class="col-lg-13 col-md-13">
						<div class="card margin-bottom-no">
							<div class="card-main">
								<div class="card-inner">
									
                                <h3>题目描述</h3><p><p><span style="font-size: medium">听说公主被关押在城堡里，彭大侠下定决心：不管一路上有多少坎坷，不管城堡中的看守有多少厉害，不管救了公主之后公主会不会再被抓走，不管公主是否漂亮、是否会钟情于自己，他将义无反顾地朝着城堡前进。</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 可是，通往城堡的路上出现了一些情况。抽象地说，假象地图在二维平面的第一象限。在每个横轴的x位置上有一个高为h<sub>x</sub>的支撑点，如果彭大侠没有跳到支撑点上，那么他就会掉下去，牺牲在路途。开始时彭大侠在起点（1，h<sub>1</sub>）处，而城堡的入口在（n，h<sub>n</sub>）处。彭大侠每次可以从支撑点（x，h<sub>x</sub>）跳到支撑点（x+1，h<sub>x+1</sub>）。但是彭大侠每次的跳跃能量只有d，也就是说，每次跳跃必须满足条件|h<sub>x+1</sub>-h<sub>n</sub>|&le;d。换句话说，如果两个相邻支撑点的纵向落差大于d，那么彭大侠就无法跳跃了！幸运的是，彭大侠还有一个杀手锏。在起点处，他可以花一个金币，把某个支撑点升高1个单位，或者降低1个单位。但是，起点处和城堡入口处的支撑点高度不能改变，并且一旦离开起点彭大侠就无法使用该杀手锏。</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 彭大侠被告知100个金币可兑换一单位生命。于是他希望通过少花金币来保存更多单位的生命。他终于找到了你这位热心的高手，请你帮他规划一下以便耗费尽量少的金币来到达城堡。</span></p>
<p></p></p><hr/><h3>输入格式</h3><p><p><span style="font-size: medium">文件第一行包含一个整数m(m&le;5)，表示问题求解次数。接下来的2m行依次表示每次求解的输入数据块。每个输入数据块占2行，其中第一行包含两个整数n和d，分别表示从起点到城堡入口处必须经过的支撑点数和每次跳跃允许的最大纵向落差，n和d之间用空格隔开，输入数据保证2&le;n&le;5000，0&le;d&le;10<sup>9</sup>；第二行包含用空格隔开的n个非负整数h<sub>1</sub>、h<sub>2</sub>、&hellip;、h<sub>n</sub>，其中h<sub>i</sub>(1&le;i&le;n)表示第i个支撑点的高度，特别地，h<sub>1</sub>表示彭大侠出发时所在支撑点的高度，h<sub>n</sub>表示城堡入口所在支撑点的高度，输入数据保证对所有1&le;i&le;n有0&le;h<sub>i</sub>&le;10<sup>9</sup>。</span></p>
<p></p></p><hr/><h3>输出格式</h3><p><p><span style="font-size: medium">有m行，第I(1&le;I&le;m)行表示第I次求解时彭大侠到达城堡必须耗费的最少金币数量。若无论怎样使用杀手锏他都无法到达城堡，则输出impossible。输入数据保证答案在int64范围之内。</span></p></p><hr/><h3>样例输入</h3><pre>3                        
10 2                      
4 5 10 6 6 9 4 7 9 8          
3 1
6 4 0
4 2
3 0 6 3

</pre><hr/><h3>样例输出</h3><pre>6
impossible
4
</pre><hr/><h3>提示</h3><p><p><span style="font-size: medium">&nbsp;对样例中的第一个输入数据块，d=2，把第三个支撑点降低3个单位，把第六个支撑点降低1个单位，把第七个支撑点升高2个单位，原序列变成：4 5 7 6 6 8 6 7 9 8，这时任意相邻支撑点的纵向落差没有超过2，彭大侠可以到达城堡！</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 对样例中的第二个输入数据块，d=1，这时不管怎样调节第二个支撑点的高度，都无法使任意相邻支撑点的纵向落差不超过1。</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 对样例中的第三个输入数据块，d=2，这时，把第二个支撑点升高1个单位，把第三个支撑点降低3个单位就满足条件了。</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 【数据规模】</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 20%&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> n&le;100</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 40%&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> n&le;1000</span></p>
<p><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> 100%&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium">&nbsp;</span><wbr></wbr><span style="font-size: medium"> n&le;5000</span></p></p><hr/><h3>题目来源</h3><p>没有写明来源</p>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				
			</section>
		</div>
	</main>

	<div class="fbtn-container">
		<div class="fbtn-inner">
			<a class="fbtn fbtn-lg fbtn-brand-accent waves-attach waves-circle waves-light waves-effect" data-toggle="dropdown" aria-expanded="true"><span class="fbtn-text fbtn-text-left">Menu</span><span class="fbtn-ori icon">apps</span><span class="fbtn-sub icon">close</span></a>
			<div class="fbtn-dropup">
				<a class="fbtn fbtn-brand waves-attach waves-circle waves-light waves-effect" href="../list.html" target="_self"><span class="fbtn-text fbtn-text-left">题目列表</span><span class="icon">menu</span></a>
				<a class="fbtn fbtn-green waves-attach waves-circle waves-effect" href="../index.html" target="_self"><span class="fbtn-text fbtn-text-left">返回主页</span><span class="icon">home</span></a>
				<a class="fbtn waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/submitpage.php?id=1484" target="_blank"><span class="fbtn-text fbtn-text-left">提交代码</span><span class="icon">send</span></a>
				<a class="fbtn fbtn-orange waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=1484" target="_blank"><span class="fbtn-text fbtn-text-left">试题讨论</span><span class="icon">chat</span></a>
				
			</div>
		</div>
	</div>

	<!-- js -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/base.min.js"></script>
	<script src="../js/project.min.js"></script>
</body>
</html>